package com.wms.controller;

import com.wms.config.ApiResponse;
import com.wms.bean.Message;
import com.wms.service.EmailService;
import com.wms.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("messages")
@CrossOrigin
public class MessageController {

    private final MessageService messageService;

    @Autowired
    public MessageController(MessageService messageService) {
        this.messageService = messageService;
    }

    // 发送消息
    @PostMapping
    public ResponseEntity<ApiResponse<?>> sendMessage(@RequestBody Message message) {
        boolean result = messageService.sendMessage(message);
        ApiResponse<?> response = new ApiResponse<>();

        if (result) {
            response.setCode(200);
            response.setMsg("消息发送成功");
        } else {
            response.setCode(500);
            response.setMsg("消息发送失败");
        }
        return ResponseEntity.ok(response);
    }

    // 根据发送者获取消息
    @GetMapping("/sender/{senderName}")
    public ResponseEntity<ApiResponse<List<Message>>> getMessagesBySender(
            @PathVariable("senderName") String senderName) {

        List<Message> messages = messageService.getMessagesBySenderName(senderName);
        ApiResponse<List<Message>> response = new ApiResponse<>();

        if (messages != null && !messages.isEmpty()) {
            response.setCode(200);
            response.setMsg("成功获取消息列表");
            response.setData(messages);
        } else {
            response.setCode(200); // 空列表也是成功
            response.setMsg("没有找到相关消息");
            response.setData(List.of()); // 返回空数组而不是null
        }
       System.out.println(response.getData());
        System.out.println(ResponseEntity.ok(response));
        return ResponseEntity.ok(response);
    }

    // 获取消息详情
    @GetMapping("/detail/{id}")
    public ResponseEntity<ApiResponse<Message>> getMessageDetail(@PathVariable("id") Long id) {
        Message message = messageService.getMessageById(id);
        ApiResponse<Message> response = new ApiResponse<>();

        if (message != null) {
            messageService.markMessageAsRead(id);
            response.setCode(200);
            response.setMsg("消息详情获取成功");
            response.setData(message);
        } else {
            response.setCode(404);
            response.setMsg("消息不存在");
        }
        return ResponseEntity.ok(response);
    }
    @Autowired
    private EmailService emailService;  // 添加这个依赖
    // 新增测试邮箱连接的接口
    @PostMapping("/test-email")
    public ResponseEntity<ApiResponse<?>> testEmailConnection(@RequestBody Map<String, String> request) {
        String recipient = request.get("recipient");
        ApiResponse<?> response = new ApiResponse<>();

        try {
            boolean result = emailService.sendEmail(
                    recipient,
                    "邮箱连接测试",
                    "<h1>邮箱服务正常</h1><p>此邮件表示系统邮箱配置正确</p>"
            );

            if (result) {
                response.setCode(200);
                response.setMsg("邮件发送成功，邮箱连接正常");
            } else {
                response.setCode(500);
                response.setMsg("邮件发送失败，请检查配置");
            }
        } catch (Exception e) {
            response.setCode(500);
            response.setMsg("连接异常: " + e.getMessage());
        }
        return ResponseEntity.ok(response);
    }

}